import pika
import time

# Connection parameters
credentials = pika.PlainCredentials('XuYixuan', 'Xyx20040422')
connection = pika.BlockingConnection(pika.ConnectionParameters('82.156.163.134', 5672, '/', credentials))
channel = connection.channel()

channel.queue_declare(queue='chat_room', durable=True)

print("=== 聊天室（输入exit退出）===")
while True:
    message = input("发送消息: ")
    if message.lower() == 'exit':
        break
    # 添加时间戳
    full_msg = f"[{time.strftime('%H:%M:%S')}] {message}"
    # 发送到chat_room队列
    channel.basic_publish(
        exchange='',
        routing_key='chat_room',
        body=full_msg.encode('utf-8'),
        properties=pika.BasicProperties(
            delivery_mode=2,  # 使消息持久化
        )
    )

connection.close()